package com.ma.lucenedemo.service.impl;

import com.ma.lucenedemo.dto.Result;
import com.ma.lucenedemo.mapper.CarMapper;
import com.ma.lucenedemo.pojo.Car;
import com.ma.lucenedemo.service.CarService;
import com.ma.lucenedemo.util.LuceneUtil;
import com.ma.lucenedemo.util.PoiUtil;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;


    @Override
    public void createIndexKu() throws IOException {
        //获取数据集
        List<Car> cars = carMapper.queryCarList();
        LuceneUtil.createIndexDataBase(cars);
    }

    @Override
    public String parseKey(String keyWord) throws IOException, ParseException {
        return LuceneUtil.parseKey(keyWord);
    }

    @Override
    public void anyParse(MultipartFile multipartFile, HttpServletResponse response) throws IOException, ParseException, InvalidFormatException {
        List<String> list = PoiUtil.parseExcelForKey(multipartFile);
        List<Result> results = LuceneUtil.parseManyKey(list);
        PoiUtil.fileAllDownload(response,results);
    }


}
